import 'dart:io';

import 'package:flutter/material.dart';
import 'package:qr_code_app/generated/assets/qr_code_app_assets.dart';
import 'package:qr_code_app/generated/route/qr_code_app_route.dart';
import 'package:qr_code_app/module/diary/bloc/log_bloc.dart';
import 'package:qr_code_app/widget/common_app_bar.dart';
import 'package:qr_code_app/widget/log_item.dart';
import 'package:todo_flutter/todo_flutter.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends BaseState<MyHomePage> {
  final ScrollController pageController = ScrollController();

  LogBloc get bloc => getBloc<LogBloc>();

  @override
  void initState() {
    super.initState();
    addBloc(LogBloc());
    bloc.init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          SingleChildScrollView(
            controller: pageController,
            child: Column(
              children: <Widget>[
                _topBitImageWidget(),
                DataChangeWidget(
                    bloc: bloc.postListBloc,
                    child: (c, s) {
                      return ListView.builder(
                        shrinkWrap: true,
                        itemCount: s?.length ?? 0,
                        padding: EdgeInsets.zero,
                        physics: const NeverScrollableScrollPhysics(),
                        itemBuilder: (_, i) {
                          return LogItemWidget(s![i]);
                        },
                      );
                    }),
              ],
            ),
          ),
          CommonAppBar(
            '好东西',
            pageController: pageController,
            menu: '添加宝贝',
            onMenuTap: () async {
              await RouterUtil.instance
                  .build(QrCodeAppRoute.publishLogPage)
                  .navigate();
              await bloc.refresh();
            },
          ),
        ],
      ),
    );
  }

  Widget _topBitImageWidget() {
    return Stack(
      children: [
        SizedBox(height: 320),
        CommonImage(
          asset: QrCodeAppAssets.defBg,
          height: 300,
          fit: BoxFit.fitHeight,
        ),
        Positioned(
            right: 16,
            bottom: 0,
            child: Row(
              children: [
                Padding(
                  padding: only(bottom: 20, right: 12),
                  child: CommonText(
                    'Persephone',
                    fontSize: 15,
                    color: Colors.white,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                CommonImage(
                  asset: QrCodeAppAssets.defaultAvatar,
                  size: 80,
                  borderRadius: BorderRadius.circular(12),
                ),
              ],
            ))
      ],
    );
  }
}
